package com.shareyi.joywindow;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.CloseWindowListener;
import org.eclipse.swt.browser.OpenWindowListener;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
import org.eclipse.swt.browser.VisibilityWindowListener;
import org.eclipse.swt.browser.WindowEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class PopWindowIE {

	/* register WindowEvent listeners */
	static void initialize(final Display display,final Browser browser) {
		
		
		browser.getShell().setImage(new Image(Display.getDefault(), 
				JoyWindowIE.class.getResourceAsStream("aa.jpg")));
		
		browser.addOpenWindowListener(new OpenWindowListener() {
			public void open(WindowEvent event) {
		//		if (!event.required) return;	/* only do it if necessary */
				Shell shell = new Shell(display);
				shell.setText("New Window");
				shell.setLayout(new FillLayout());
				Browser browser = new Browser(shell,SWT.NONE);
				initialize(display, browser);
				((WindowEvent)event).browser = browser;
			}
		});
		browser.addVisibilityWindowListener(new VisibilityWindowListener() {
			public void hide(WindowEvent event) {
				Browser browser = (Browser)event.widget;
				Shell shell = browser.getShell();
				shell.setVisible(false);
			}
			public void show(WindowEvent event) {
				Browser browser = (Browser)event.widget;
				final Shell shell = browser.getShell();
				if (event.location != null) shell.setLocation(event.location);
				if (event.size != null) {
					Point size = event.size;
					shell.setSize(shell.computeSize(size.x, size.y));
				}
				shell.open();
			}
		});
		browser.addCloseWindowListener(new CloseWindowListener() {
			public void close(WindowEvent event) {
				Browser browser = (Browser)event.widget;
				Shell shell = browser.getShell();
				shell.close();
			}
		});
		
		browser.addTitleListener(new TitleListener() {
			public void changed(TitleEvent event) {
				if(event.title!=null&&event.title.trim().length()!=0)
					browser.getShell().setText("joyWindow("+event.title+")");
			}
		});
		
		
		
		Menu m1=new Menu(browser);
		MenuItem copyItem=new MenuItem(m1, SWT.POP_UP);
		copyItem.setText("c(复制)");
		copyItem.addSelectionListener(new SelectionListener() {
			
			public void widgetSelected(SelectionEvent arg0) {
				browser.execute("var sText = window.getSelection ? window.getSelection() : document.selection.createRange().text;\n"+
				"if(sText!=null&&sText!='')\nwindow.clipboardData.setData('text',sText);");
			}
			
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		MenuItem pasteItem=new MenuItem(m1, SWT.POP_UP);
		pasteItem.setText("p(粘贴)");
		pasteItem.addListener(SWT.Selection, new Listener() {
			
			public void handleEvent(Event arg0) {
				browser.execute("var CopiedTxt = document.selection.createRange();\n"+
				"if(document.activeElement.type)\n"+
				"if(CopiedTxt)CopiedTxt.execCommand('paste');");
			}
		});
		browser.setMenu(m1);
		
		
	}
	
}
